//
//  OrderViewController.m
//  ForestPack
//
//  Created by 骆锴 on 2018/7/18.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "OrderViewController.h"
#import "DemandsViewCell.h"
#import "InputViewCell.h"
#import "ConfirmViewCell.h"

@interface OrderViewController ()<UITextFieldDelegate,UITextViewDelegate>

@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, assign) BOOL isSuccess;

@property (nonatomic, strong) UITextView *demandsvalue;
@property (nonatomic, strong) UITextField *num;
@property (nonatomic, strong) UITextField *phone;
@property (nonatomic, strong) UITextField *qq;

@end

@implementation OrderViewController

- (void)viewDidLoad {
    _isSuccess = NO;
    [super viewDidLoad];
    [self setTitle:@"帮我下单"];
    [self setUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setUI{
    UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT - ScreenX375(64))];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(releasefirstResponder)];\
    [bg addGestureRecognizer:tap];
    bg.backgroundColor = [UIColor whiteColor];
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(16), ScreenX375(58), ScreenX375(14))];
    title.font = Kfont(14);
    title.text = @"需    求  :";
    title.textColor = [UIColor blackColor];
    [bg addSubview:title];
    
    _demandsvalue = [[UITextView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(39), ScreenX375(349), ScreenX375(112))];
    _demandsvalue.textColor = REXADECIMALCOLOR(@"#999999");
    _demandsvalue.text = @"请尽可能描述您的需求(包装材质、印刷工艺、尺寸、用途)";
    _demandsvalue.delegate = self;
    _demandsvalue.textAlignment = NSTextAlignmentLeft;
    _demandsvalue.font = Kfont(12.0);
    _demandsvalue.layer.cornerRadius = 6.0;
    _demandsvalue.layer.borderWidth = 1.0;
    _demandsvalue.layer.borderColor = REXADECIMALCOLOR(@"#dcdcdc").CGColor;
    [bg addSubview:_demandsvalue];
    
    NSArray *titleArray = [[NSArray alloc] initWithObjects:@"数    量  :",@"手 机 号 :",@"Q Q  号 :", nil];
    for (int i = 0; i < 3 ; i++) {
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(165 + 44 * i), ScreenX375(65), ScreenX375(14))];
        title.font = Kfont(14);
        title.text = [titleArray objectAtIndex:i ];
        title.textColor = [UIColor blackColor];
        [bg addSubview:title];

        UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(194 + 44 * i), ScreenX375(349), ScreenX375(1))];
        UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"虚线"]];
        [line setBackgroundColor:color];
        [bg addSubview:line];
    }
    
    _num = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(81), ScreenX375(165 + 44 * 0), ScreenX375(200), ScreenX375(15))];
    _num.borderStyle = UITextBorderStyleNone;
    _num.textAlignment = NSTextAlignmentLeft;
    _num.placeholder = @"请填写数量(100个起印)";
    _num.font = Kfont(15);
    [bg addSubview:_num];
    
    _phone = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(81), ScreenX375(165 + 44 * 1), ScreenX375(200), ScreenX375(15))];
    _phone.borderStyle = UITextBorderStyleNone;
    _phone.textAlignment = NSTextAlignmentLeft;
    _phone.placeholder = @"方便客服与您联系";
    _phone.font = Kfont(15);
    [bg addSubview:_phone];
    
    _qq = [[UITextField alloc] initWithFrame:CGRectMake(ScreenX375(81), ScreenX375(165 + 44 * 2), ScreenX375(200), ScreenX375(15))];
    _qq.borderStyle = UITextBorderStyleNone;
    _qq.textAlignment = NSTextAlignmentLeft;
    _qq.placeholder = @"方便客服与您联系";
    _qq.font = Kfont(15);
    [bg addSubview:_qq];
    
    UILabel *warning = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(295), ScreenX375(336), ScreenX375(13))];
    warning.textAlignment = NSTextAlignmentCenter;
    warning.text = @"请确保信息正确无误,以便印前人员与您及时联系确认印刷需求";
    warning.textColor = ThemeColor;
    warning.font = Kfont(12);
    [bg addSubview:warning];
    
    UIButton *submit = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(367), ScreenX375(349), ScreenX375(44))];
    submit.layer.cornerRadius = 22;
    submit.backgroundColor = ThemeColor;
    [submit setTitle:@"提交" forState:UIControlStateNormal];
    [submit addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    [bg addSubview:submit];
    
    [self.view addSubview:bg];
}

-(void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@"请尽可能描述您的需求(包装材质、印刷工艺、尺寸、用途)"]) {
        textView.textColor = [UIColor blackColor];
        textView.text = @"";
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length < 1) {
        textView.textColor = REXADECIMALCOLOR(@"#999999");
        textView.text = @"请尽可能描述您的需求(包装材质、印刷工艺、尺寸、用途)";
    }
}

-(void)sentOrder{
    if (_demandsvalue.text.length == 0) {
        [HttpManager showNoteMsg:@"请完整填写信息"];
    }else if (_phone.text.length == 0){
        [HttpManager showNoteMsg:@"请完整填写信息"];
    }else if (_qq.text.length == 0){
        [HttpManager showNoteMsg:@"请完整填写信息"];
    }else if (_num.text.length ==0){
        [HttpManager showNoteMsg:@"请完整填写信息"];
    }else{
        if ([_num.text integerValue] < 100) {
            [HttpManager showNoteMsg:@"数量需要100起订，请修改"];
            return;
        }
        NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"token":[FPLoginAndRegisterTools getToken],@"xuqiu":_demandsvalue,@"telephone":_phone,@"qq_num":_qq,@"num":_num}];
        [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/solution_product",REQUESTHEADER] andParameter:parameter success:^(id successResponse){
            NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
            if ([status isEqualToString:@"1"]) {
                self->_isSuccess = YES;
                [HttpManager showNoteMsg:@"提交成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }
            else {
                [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
            }
        } andFailure:^(id failureResponse) {
            [HttpManager showFail];
        }];
    }
}

-(void)submit{
    [self sentOrder];
}


-(void)releasefirstResponder{
    [_demandsvalue resignFirstResponder];
    [_num resignFirstResponder];
    [_phone resignFirstResponder];
    [_qq resignFirstResponder];
}

@end
